package com.fhao.spring.selfblog.controller;

import com.fhao.spring.selfblog.constant.Constants;
import com.fhao.spring.selfblog.model.BlogInfo;
import com.fhao.spring.selfblog.model.Result;
import com.fhao.spring.selfblog.service.BlogService;
import com.fhao.spring.selfblog.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {

    @Autowired
    private BlogService blogService;

    @RequestMapping("/getList")
    public List<BlogInfo> getList() {
        log.info("getList 获取博客列表...");
        return blogService.getList();
    }

    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId,HttpServletRequest request) {
        log.info("getBlogDetail 获取博客详情");
        BlogInfo blogDetail = blogService.getBlogDetail(blogId);
        if(blogDetail == null) {
            return null;
        }
        // 从token中 获取作者id
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        Integer userId = JwtUtils.getIdByToken(token);
        //判断当前登录用户的id 和 博客的作者 id是否一致.
        if(userId != null && userId == blogDetail.getUserId()) {
            blogDetail.setLoginUser(true);
        }
        return blogDetail;
    }

    //后端接收的是 json,所以要用 @RequestBody
    @RequestMapping("/add")
    public Result addBook(@RequestBody BlogInfo blogInfo, HttpServletRequest request) {
        //打印日志
        log.info("添加博客,blogInfo : {}",blogInfo);
        //1. 参数校验
        if(!StringUtils.hasLength(blogInfo.getTitle()) || !StringUtils.hasLength(blogInfo.getContent())) {
            return Result.fail("标题和内容不能为空");
        }
        //2. 从token中 获取作者id
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        Integer userId = JwtUtils.getIdByToken(token);
        if(userId == null || userId <= 0) {
            return Result.fail("内部错误请联系管理员");
        }
        blogInfo.setUserId(userId);
        Boolean result = blogService.insertBook(blogInfo);
        return Result.success(result);
    }

    //删除博客
    @RequestMapping("/delete")
    public Boolean deleteBlog(Integer blogId) {
        log.info("删除博客,blogId: {}",blogId);
        //参数校验
        if(blogId == null || blogId <= 0) {
            return false;
        }
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        return blogService.update(blogInfo);
    }

    //更新博客
    @RequestMapping("/update")
    public Boolean updateBlog(BlogInfo blogInfo) {
        log.info("更新博客,blogInfo:{}",blogInfo);
        //1.参数校验
        if(blogInfo == null
                || blogInfo.getId() == null
                || !StringUtils.hasLength(blogInfo.getTitle())
                || !StringUtils.hasLength(blogInfo.getContent())) {
            return false;
        }
        return blogService.update(blogInfo);
    }
}



















